package com.fire.social.service.impl;

import com.fire.social.entity.node.Content;
import com.fire.social.entity.node.User;
import com.fire.social.mapper.ContentMapper;
import com.fire.social.mapper.FollowMapper;
import com.fire.social.mapper.RecommendMapper;
import com.fire.social.mapper.UserMapper;
import com.fire.social.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private RecommendMapper recommendMapper;

    @Override
    public List<User> findAll() {
        return userMapper.getAllUser();
    }

    @Override
    public User loginUser(String account, String password) {
        return userMapper.login(account, password);
    }

    @Override
    public void getContent(User user, Map<String, Object> paramMap) {
        // 获取关注的总数
        Long iFollowNum = followMapper.countIFollow(user.getAccount());
        // 获取被关注的总数
        Long followMeNum = followMapper.countFollowMe(user.getAccount());
        // 读取动态内容
        List<Content> contentList = contentMapper.getFriendContents(user.getAccount());
        // 读取推荐用户列表
        List<User> byfriend = recommendMapper.byFriend(user.getAccount()).stream().limit(4).collect(Collectors.toList());
        List<User> bycontent = recommendMapper.bycontent(user.getAccount()).stream().limit(4).collect(Collectors.toList());
        List<User> byhobby = recommendMapper.byHobby(user.getAccount()).stream().limit(4).collect(Collectors.toList());

        paramMap.put("byfriend", byfriend);
        paramMap.put("byshare", bycontent);
        paramMap.put("byhobby", byhobby);

        paramMap.put("shares", contentList);
        paramMap.put("user", user);
        paramMap.put("myfollowing", iFollowNum);
        paramMap.put("follower", followMeNum);
    }

    @Override
    public User createAccount(User user) {
        return userMapper.createUser(user.getAccount(), user.getPassword(),
                                     user.getNickname(), user.getAge(),
                                     user.getGender(), user.getEmail(), user.getImgurl());
    }

    @Override
    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }
}
